<?php
namespace Admin\Controller;

class MenteesController extends AdminController
{
    #列表
    function index()
    {
        $nickname = I("nickname");
        $city = I("city");
        $state = I("state");
        $code = I("code");
        if ($nickname) {
            $map['nickname'] = array('like', "%" . $nickname . "%");
            $this->assign("nickname", $nickname);
        }
        if ($city) {
            $map['city'] = $city;
            $this->assign("city", $city);
        }
        if ($state) {
            $map['state'] = $state;
            $this->assign("state", $state);
        }
        if ($code) {
            $map['zip'] = $code;
            $this->assign("code", $code);
        }
        $map['del'] = 0;
        $obj = M('Mentees');
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('keyword', $keyword);
        $stateList = M('States')->select();
        $this->assign('stateList', $stateList);
        $this->display();
    }
    #编辑
    public function detail()
    {
        $id = (int) $_GET['id'];
        if (!isset($id)) {
            $this->error('Parameter error');
        }
        $obj = M('Mentees');
        $list = $obj->where("id={$id}")->find();
        if (!$list) {
            $this->error('Information does not exist');
        } else {

            $state = M('States')->select();
            $this->assign('state',$state);
            
            $this->assign('list', $list);
            $this->display();
        }
    }


    #删除
    public function del()
    {
        $id = I('post.selectedids');
        if ($id == '') {
            $state = "You do not select any information!";
        } else {
            $obj = M('Mentees');
            foreach ($id as $v) {
                $map['id'] = $v;
                $obj->where($map)->delete();
            }
            $state = 'SUCCESS';
            $message = 'Successful operation';
            $url = U('Mentees/index');
        }
        echo $this->echo_json_str($state, $message, $url);
    }

    //添加学员
   /* public function add()
    {
        $state = M('States')->select();
        $this->assign('state', $state);
        $nowYear = date('Y') - 10;
        $this->assign('nowYear', $nowYear);
        $this->display();
    }
    public function save()
    {
        $obj = M('Member');
        if ($data = $obj->create()) {
            $map['username'] = $data['username'];
            $list = $obj->where($map)->find();
            if ($list) {
                echo $this->echo_json_str('The email address is already exist.', $message, $url);
                die;
            }
            unset($map);
            $map['nickname'] = I('post.nickname');
            $list = M('Mentees')->where($map)->find();
            if ($list) {
                echo $this->echo_json_str('Validation of the nickname be repeated by other profile nickname', $message, $url);
                die;
            }
            $data['password'] = md5($data['password']);
            $data['userType'] = 1;
            $data['basic'] = 1;
            $data['status'] = 1;
            $data['regDate'] = time();
            $data['regIP'] = get_client_ip();
            $data['loginDate'] = time();
            $data['loginIP'] = get_client_ip();
            if ($userID = $obj->add($data)) {
                $data['nickname'] = I('post.nickname');
                $data['firstName'] = I('post.firstName');
                $data['lastName'] = I('post.lastName');
                $data['gender'] = I('post.gender');
                $data['birthday'] = I('post.birthday');
                $data['address'] = I('post.address');
                $data['city'] = I('post.city');
                $data['state'] = I('post.state');
                $data['zip'] = I('post.zip');
                $data['phone'] = I('post.phone');
                $data['school'] = I('post.school');
                $data['major'] = I('post.major');
                $data['desired'] = I('post.desired');
                $data['description'] = I('post.description');
                $data['userid'] = $userID;
                $data['updateTime'] = time();
                $data['createTime'] = time();
                $list = M('Mentees')->add($data);
                if ($list) {
                    $state = 'SUCCESS';
                    $message = L('_action_success');
                    $url = U('Mentees/index');
                } else {
                    $obj->where('userid=' . $userID)->delete();
                    $state = L('_action_fail');
                }
            } else {
                $state = L('_action_fail');
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state, $message, $url);
    }*/
    //更新状态
    public function update()
    {
        $obj = M('Mentees');
        if ($data = $obj->create()) {

            //昵称
            if ($data['nickname']!='') {
                # code...          
                $map['nickname'] = $data['nickname'];
                $map['userid'] = array('neq',$data['userid']);
                $r = $obj->where($map)->find();
                if ($r) {
                    echo $this->echo_json_str('Validation of the nickname be repeated by other profile nickname',$message,$url); 
                    die;
                }
            }       

            $data['updateTime'] = time();

            if ($list = $obj->save($data)) {                
                $state = 'SUCCESS';
                $message = L('_action_success');
                $url = U('Mentees/index');
            } else {                
                $state = L('_action_fail');                
            }      
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }
    //选择导师
    public function select()
    {
        $menteesid = I("menteesid");
        $trainID = I("train");
        if ($trainID) {
            $map['trainItem'] = array("like", "%-" . $trainID . "-%");
            $this->assign("trainID", $trainID);
        }
        $map['del'] = 0;
        //$map['status'] = 1;
        $obj = M('Mentors');
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $value['trainItem'] = trainStrtoArr($value['trainItem']);
            $where['id'] = array('in', $value['trainItem']);
            $list[$key]['trainItem'] = M('TrainCate')->field('name')->where($where)->select();
            unset($map);
            $map['status'] = 0;
            $map['mentorsID'] = $value['userid'];
            $list[$key]['students'] = M("Train")->where($map)->count();
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        unset($map);
        $map['show'] = 1;
        $train = M('TrainCate')->where($map)->order('sort asc')->select();
        $this->assign('train', $train);
        $this->assign('menteesid', $menteesid);
        $this->display();
    }
    public function selected()
    {
        $mentorsid = I("mentorsid");
        $menteesid = I("menteesid");
        if ($menteesid == "" || $mentorsid == "") {
            $this->error("Parameter error");die;
        } else {
        	$map['menteesID'] = $menteesid;
        	$map['mentorsID'] = $mentorsid;
        	if (M("Train")->where($map)->find()) {
        		$this->error("Repeat binding");die;
        	}

            $data['menteesID'] = $menteesid;
            $data['mentorsID'] = $mentorsid;
            $data['status'] = 0;
            $data['createTime'] = time();
            if (M("Train")->add($data)) {
                $this->success(L('_action_success'));
            } else {
                $this->error(L('_action_fail'));
            }
        }
    }
}